package com.djzhou.gmms.service.position.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;

import com.djzhou.gmms.model.position.TbPosition;
import com.djzhou.gmms.model.entity.PageBean;
import com.djzhou.gmms.model.position.TbPosition;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.gmms.service.base.impl.BaseServiceImpl;
import com.djzhou.gmms.service.position.PositionService;


public class PositionServiceImpl  extends BaseServiceImpl implements PositionService{

	@Override
	public void add(TbPosition t) {
		positionDAO.insert(t);
		
	}

	@Override
	public void delete(Integer id) {
		TbPosition position=positionDAO.queryByID(TbPosition.class, id);
		position.setDeleteFlag(1);
		positionDAO.update(position);
		
	}

	@Override
	public TbPosition queryById(Integer id) {
		return positionDAO.queryByID(TbPosition.class, id);
	}

	@Override
	public List<TbPosition> queryList(HashMap<String, Object> hashMap) {
		String hql="from TbPosition position where position.deleteFlag=0";
		return positionDAO.query(hql);
	}

	@Override
	public PageBean<TbPosition> queryPageBean(HashMap<String, Object> hashMap,
			int pageSize, int currentPage) {
		String positionId=null;//通过部门查询职位
		String companyId=null;//通过部门查询职位
		String hql="from TbPosition position where position.deleteFlag=0";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		if(hashMap!=null && hashMap.size()!=0){
			if(hashMap.get("positionId")!=null){
				positionId=hashMap.get("positionId").toString();
				sb.append(" and position.positionId=").append(positionId);
			}
			if(hashMap.get("departmentId")!=null){
				sb.append(" and position.department.departmentId=").append(hashMap.get("departmentId").toString());
			}
			if(hashMap.get("companyId")!=null){
				companyId=hashMap.get("companyId").toString();
				sb.append(" and position.company.companyId=").append(companyId);
			}
		}
		sb.append(" order by createTime desc");
		hql=sb.toString();
		PageBean<TbPosition> pageBean=positionDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public void update(TbPosition t) {
		positionDAO.update(t);
		
	}
	@Override
	public VoResource query4AutoComplete(HashMap<String, Object> hashMap, String sidx,
			String sord, int pageSize, int currentPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TbPosition> queryList(Integer companyId) {
		if(companyId!=1){
			String hql="from TbPosition position where position.deleteFlag=0 and position.positionId<>1 and position.company.companyId in(1,?)";
			return positionDAO.query(hql,companyId);
		}else{
			String hql="from TbPosition position where position.deleteFlag=0";
			return positionDAO.query(hql);
		}
	}

	@Override
	public void updatePositionDefault(Integer positionId) {
		String hql="update TbPosition set defaultFlag=0 where positionId<>?";
		positionDAO.update(hql, positionId);
		TbPosition position=positionDAO.queryByID(TbPosition.class, positionId);
		position.setDefaultFlag(1);
		positionDAO.update(position);
	}

}
